package com.tanhua.dubbo.server.api;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.Users;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service(version = "1.0.0")
public class UsersApiImpl implements UsersApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * 保存好友关系
     *
     * @param userId   用户id
     * @param friendId 好友id
     * @return
     */
    @Override
    public String saveUsers(Long userId, Long friendId) {
        if (ObjectUtil.isAllEmpty(userId, friendId)) {
            return null;
        }

        //检测是否该好友关系是否存在
        Query query = Query.query(Criteria.where("userId").is(userId)
                .and("friendId").is(friendId));
        long count = mongoTemplate.count(query, Users.class);
        if (count > 0) {
            return null;
        }

        Users users = new Users();
        users.setId(ObjectId.get());
        users.setDate(System.currentTimeMillis());
        users.setUserId(userId);
        users.setFriendId(friendId);

        //注册我与好友的关系
        mongoTemplate.save(users);

        //注册好友与我的关系
        users.setId(ObjectId.get());
        users.setUserId(friendId);
        users.setFriendId(userId);
        mongoTemplate.save(users);

        return users.getId().toHexString();
    }


    /**
     * 删除好友数据
     *
     * @param userId   用户id
     * @param friendId 好友id
     * @return
     */
    @Override
    public Boolean removeUsers(Long userId, Long friendId) {
        //删除我与好友的关系数据
        Query query1 = Query.query(Criteria.where("userId").is(userId)
                .and("friendId").is(friendId));
        long count1 = mongoTemplate.remove(query1, Users.class).getDeletedCount();

        //删除好友与我的关系数据
        Query query2 = Query.query(Criteria.where("userId").is(friendId)
                .and("friendId").is(userId));
        long count2 = mongoTemplate.remove(query2, Users.class).getDeletedCount();


        return count1 > 0 && count2 > 0;
    }


    /**
     * 根据用户id查询全部Users列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<Users> queryAllUsersList(Long userId) {
        Query query=Query.query(Criteria.where("userId").is(userId));
        return mongoTemplate.find(query,Users.class);
    }

    /**
     * 根据用户id查询Users列表(分页查询)
     *
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Users> queryUsersList(Long userId, Integer page, Integer pageSize) {
        PageRequest pageRequest=PageRequest.of(page-1,pageSize, Sort.by(Sort.Order.desc("created")));

        Query query=Query.query(Criteria.where("userId").is(userId)).with(pageRequest);
        List<Users> usersList = mongoTemplate.find(query, Users.class);

        PageInfo<Users> pageInfo=new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setRecords(usersList);

        return pageInfo;
    }
}
